<template>
  <div>
    <el-card>
      <template #header>
        <h4>账号列表</h4>
      </template>
      <div>
        <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column prop="account" label="账号"> </el-table-column>
          <el-table-column prop="userGroup" label="用户组"> </el-table-column>
          <el-table-column label="创建时间">
            <template v-slot="{ row }">{{ row.ctime | dateFilter }}</template>
          </el-table-column>
          <el-table-column label="操作" show-overflow-tooltip>
            <template v-slot="{ row }">
              <el-button size="mini" @click="echo(row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="del(row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <!-- 编辑框 -->
        <el-dialog
          title="编辑账号"
          :visible.sync="dialogFormVisible"
          width="500px"
        >
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="账号" prop="account">
              <el-input v-model="ruleForm.account"></el-input>
            </el-form-item>
            <el-form-item label="用户组" prop="userGroup">
              <el-select
                v-model="ruleForm.userGroup"
                placeholder="请选择用户组"
              >
                <el-option label="超级管理员" value="超级管理员"></el-option>
                <el-option label="普通管理员" value="普通管理员"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="redact()">确 定</el-button>
          </div>
        </el-dialog>

        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
        <el-button type="danger" @click="batchDel">批量删除</el-button>
        <el-button type="primary" @click="toggleSelection()"
          >取消选择</el-button
        >
      </div>
    </el-card>
  </div>
</template>

<script>
import { accList, accDel, batchdel, edit } from '../../api/info.js'
// import filters from '../../utils/filters.js'
import moment from 'moment'
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      multipleSelection: [],
      // 编辑
      dialogFormVisible: false,
      ruleForm: {
        account: '',
        userGroup: ''
      },
      rules: {
        account: [
          { required: true, message: '请输入账号名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        userGroup: [
          { required: true, message: '请选择管理权限', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },

  filters: {
    dateFilter(res) {
      return moment(res).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  methods: {
    // 列表请求
    async getList() {
      const data = await accList({
        currentPage: this.currentPage, // 当前页码
        pageSize: this.pageSize // 每页条数
      })
      console.log(data)
      this.tableData = data.data
      this.total = data.total
      // console.log(this.pageSizes, this.pageSize)
    },
    // 时间过滤

    // 取消选择
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
      console.log(`当前页: ${val}`)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
      console.log(`每页 ${val} 条`)
    },
    // 删除账号
    del(id) {
      this.$confirm('此操作将永久删除该账号, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const data = await accDel({ id: id })
          console.log(data)
          if (data.code === 0) {
            this.getList()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    handleSelectionChange(val) {
      const arr = []
      val.forEach(item => {
        arr.push(item.id)
      })
      this.multipleSelection = arr
    },

    // 批量删除
    batchDel() {
      this.$confirm('此操作将永久删除这些账号, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const data = await batchdel({
            ids: JSON.stringify(this.multipleSelection)
          })
          console.log(data)
          if (data.code === 0) {
            this.getList()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    // 数据回显
    echo(data) {
      console.log(data)
      this.ruleForm.id = data.id
      this.ruleForm.account = data.account
      this.ruleForm.userGroup = data.userGroup
      this.dialogFormVisible = true
    },
    // 编辑确定
    async redact() {
      this.dialogFormVisible = false
      const data = await edit(this.ruleForm)
      console.log(data)
      if (data.code === 0) {
        this.getList()
      }
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-form-item__content .el-input__inner {
  width: 300px;
}
.el-pagination {
  margin: 20px 0;
}
</style>
